<?php

namespace app\api\controller;

use app\admin\model\Theme;
use app\common\controller\Api;
use app\common\model\Product as ProductModel;
use app\common\model\ProductCategory;

class Product extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /** 分类 */
    public function category()
    {
        $list = ProductCategory::all();
        $this->success('分类列表', $list, 1, $this->serial_number);
    }

    /** 主题 */
    public function theme()
    {
        $list = Theme::all();
        $this->success('主题列表', $list, 1, $this->serial_number);
    }

    /** 商品列表 */
    public function list()
    {
        $param = $this->request->param();
        $this->validateCommon('Product.list', $this->serial_number);
        $sort            = $param['sort'] ?? 1; // 1默认最新时间  2热销   3价格从低到高  4价格从高到低
        $where['status'] = 1;
        if (isset($param['themeId']) && !empty($param['themeId'])) { //主题ID
            $where['theme_id'] = $param['themeId'];
        }
        if (isset($param['categoryId']) && !empty($param['categoryId'])) { //分类ID
            $where['category_id'] = $param['categoryId'];
        }
        if ($sort == 1) $orderMemo = "create_time desc";
        if ($sort == 2) $orderMemo = "is_hot asc";
        if ($sort == 3) $orderMemo = "unit_price asc";
        if ($sort == 4) $orderMemo = "unit_price desc";
        $list = ProductModel::where($where)
                            ->order($orderMemo)
                            ->field('id productId,product_name productName,unit_price productPrice,real_price productRealPrice,condition'
                                    . ',product_main_image productMainImage,product_memo productMemo'
                            )
                            ->paginate($param['pageCount'], false, ['page' => $param['page']]);
        $this->success('商品列表', $list, 1, $this->serial_number);
    }

    /** 商品详情 */
    public function detail()
    {
        $param = $this->request->param();
        $this->validateCommon('Product.detail', $this->serial_number);
        $field       = "id productId,product_name productName,unit_price productPrice,real_price productRealPrice,condition,product_images productImages,product_banners productBanners"
                       . ",product_style productStyle,product_detail productDetail,product_memo productMemo,product_text productText,create_time createTime";
        $productInfo = ProductModel::where('id', $param['productId'])->field($field)->find();
        if (!$productInfo) $this->error('商品信息获取失败', '', 0, $this->serial_number);
        $productInfo['productImages']  = explode(',', $productInfo['productImages']);
        $productInfo['productBanners'] = explode(',', $productInfo['productBanners']);
        $productInfo['productStyle']   = json_decode($productInfo['productStyle'], true);
        $newStyle                      = [];
        foreach ($productInfo['productStyle'] as $K => $value) {
            $newStyle[] = $K;
        }
        $newImages = [];
        foreach ($productInfo['productImages'] as $K => $value) {
            $newImages[$K] = getCurrentUrl() . $value;
        }
        $newBanners = [];
        foreach ($productInfo['productBanners'] as $K => $value) {
            $newBanners[$K] = getCurrentUrl() . $value;
        }
        $productInfo['productStyle']   = $newStyle;
        $productInfo['productImages']  = $newImages;
        $productInfo['productBanners'] = $newBanners;
        $this->success('商品详情', $productInfo, 1, $this->serial_number);
    }

    /** 计费 */
    public function charging()
    {
        $param = $this->request->param();
        $this->validateCommon('Product.charging', $this->serial_number);
        $productInfo = ProductModel::get($param['productId']);
        if(!$productInfo) $this->error('商品不存在','',0,$this->serial_number);
        $productRealPrice = bcmul($productInfo->real_price,$param['productNum'],2);
        $productUnitPrice = bcmul($productInfo->unit_price,$param['productNum'],2);
        $this->success('计费价格',['realPrice'=>$productRealPrice,'unitPrice'=>$productUnitPrice],1,$this->serial_number);
    }

}